package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author zhoujiao
 * @create 2022-01-19-09:41
 */
public class SignetManagementQuantityAttrHandler extends AbstractAttrHandler<KiteInst, CustAttrData> {
    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteInst inst, CustAttrData data) {
        // 融和追加和产品变更  型号、销售价和订购数量都置灰
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), "2826","2024")
            &&  KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_YFYZGL_PROD))) {
            inst.getAttrMap().get("200001696").setIsEdit(KeyConsts.IFTRUE_F);
            inst.getAttrMap().get("200004107").setIsEdit(KeyConsts.IFTRUE_F);
            inst.getAttrMap().get("200002772").setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
        super.after(attr, inst, data);
    }


    @Override
    public void def(KiteAttr attr, KiteInst inst, CustAttrData data) {
        // 附加商品订购数量联动
        String sceneInstId = data.getSceneInstId();
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        for (KiteProdInst kiteProdInst : kiteProdInstList) {
            // 根据主框架的订购数量 实现联动
            if("63249669".equals(kiteProdInst.getSpecId())){      // 63249669为雁飞印章管理套件-框架主产品的prodId
                KiteAttr quantityKiteAttr = kiteProdInst.getAttr("200001696");
                String quantity = quantityKiteAttr.getValue();
                attr.setDefaultValue(quantity);
                break;
            }
        }
        super.def(attr, inst, data);
    }
}

